package cn.edu.gzmtu.controller;

import cn.edu.gzmtu.domain.Orders;
import cn.edu.gzmtu.service.IOrdersService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private IOrdersService ordersService;

    // /**
    //   * 查询所有订单
    //   *
    //   * @return
    //   * @t hrows Exception
    //   */
    // @RequestMapping("/findAll.do")
    // public ModelAndView findAll() throws Exception {
    //
    //     ModelAndView mv = new ModelAndView();
    //
    //     List<Orders> ordersList = ordersService.findAll();
    //     System.out.println("ordersList = " + ordersList);
    //
    //     mv.addObject("ordersList", ordersList);
    //     mv.setViewName("orders-list");
    //
    //     return mv;
    // }

    /**
     * 查询所有订单
     * 带分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @RequestMapping("/findAll.do")
    public ModelAndView findAll(@RequestParam(name = "pageNum", required = true, defaultValue = "1") Integer pageNum,
                                @RequestParam(name = "pageSize", required = true, defaultValue = "3") Integer pageSize) throws Exception {

        ModelAndView mv = new ModelAndView();

        List<Orders> ordersList = ordersService.findAll(pageNum, pageSize);

        // PageInfo是一个分页Bean，里面存储了所有的分页信息
        PageInfo pageInfo = new PageInfo(ordersList);

        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("orders-page-list");

        return mv;
    }

    /**
     * 根据id查询订单详情
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("findById.do")
    public ModelAndView findById(@RequestParam(name="id", required = true) String id) throws Exception {
        ModelAndView mv = new ModelAndView();

        Orders orders = ordersService.findById(id);
        System.out.println("orders = "+orders);

        mv.addObject("orders", orders);
        mv.setViewName("orders-show");

        return mv;
    }
}
